package com.jiangyao.controller;

import com.jiangyao.common.ApiResult;
import com.jiangyao.common.exception.ServerException;
import com.jiangyao.common.utils.FtpClient;
import com.jiangyao.component.RedisComponent;
import com.jiangyao.model.dto.ClassInfoDto;
import com.jiangyao.model.dto.ClassUserDto;
import com.jiangyao.model.dto.CollectDto;
import com.jiangyao.model.dto.OrgInfoDto;
import com.jiangyao.model.dto.backend.OrgClassApplyListDTO;
import com.jiangyao.model.entity.ClassInfo;
import com.jiangyao.model.entity.ClassUser;
import com.jiangyao.model.entity.OrgInfo;
import com.jiangyao.model.vo.CollectVo;
import com.jiangyao.model.vo.JobVo;
import com.jiangyao.model.vo.OrgInfoVo;
import com.jiangyao.model.vo.UserSessionVO;
import com.jiangyao.model.vo.backend.OrgClassApplyListVO;
import com.jiangyao.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/org")
@Api(tags = "组织相关接口")
public class OrgInfoController extends BaseController {

    @Value("${jiangyao.defaultOrgId}")
    private Integer defaultOrgId;

    @Value("${jiangyao.defaultClassId}")
    private Integer defaultClassId;

    @Autowired
    private DozerBeanMapper dozerBeanMapper;

    @Autowired
    private RedisComponent redisCompnent;

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private ClassInfoService classInfoService;

    @Autowired
    private ClassUserService classUserService;

    @Autowired
    private CollectService collectService;

    @Autowired
    private OrgClassApplyService orgClassApplyService;

    @ApiOperation(value = "获取有效组织", notes = "选填字段：orgName")
    @PostMapping("/getOrgListByParams")
    public ApiResult<List<OrgInfo>> getOrgListByParams(@RequestBody OrgInfoDto queryDto) {
        queryDto.setStatus(1);
        return new ApiResult(orgInfoService.getList(queryDto));
    }

    @ApiOperation(value = "获取组织下的班级", notes = "必填字段：orgId")
    @PostMapping("/getOrgClassByParams")
    public ApiResult<List<ClassInfo>> getOrgClassByParams(@RequestBody ClassInfoDto queryDto) {
        if(queryDto.getOrgId()== null || queryDto.getOrgId()<0){
            throw new ServerException(305, "需传入有效的组织ID");
        }
        queryDto.setStatus(1);
        return new ApiResult(classInfoService.getOrgClassByParams(queryDto));
    }

    @ApiOperation(value = "申请加入组织或班级", notes = "必填字段：orgId，userId")
    @PostMapping("/applyForOrg")
    public ApiResult<Boolean> applyForOrg(@Validated({ClassUserDto.Add.class}) @RequestBody ClassUserDto classUserDto) {
        return new ApiResult<>(orgClassApplyService.applyForOrg(classUserDto));
    }

    @ApiOperation(value = "申请记录", notes = "无传参")
    @PostMapping("/applyList")
    public ApiResult<List<OrgClassApplyListVO>> applyList(@RequestBody OrgClassApplyListDTO dto) {
        return new ApiResult<>(orgClassApplyService.getApiList(dto));
    }

    @ApiOperation(value = "加入默认的组织和班级", notes = "无传参")
    @PostMapping("/addDefaultClass")
    public ApiResult<Boolean> addDefaultClass() {
        Integer userId = getLoginSessionUserId();
        ClassUser entity = new ClassUser();
        entity.setApplyType(3);
        entity.setClassId(defaultClassId);
        entity.setOrgId(defaultOrgId);
        entity.setUserId(userId);
        entity.setCreateTime(new Date());
        entity.setStatus(1);
        return new ApiResult<>(classUserService.save(entity));
    }


    /**
     * 获取组织详情
     * @param org
     * @return
     */
    @ApiOperation(value = "获取组织详情",notes = "必填字段：orgId")
    @PostMapping("/getOrgDetail")
    public ApiResult<OrgInfo> getOrgDetail(@RequestBody OrgInfo org){
        if(org.getOrgId()==null){
            throw new ServerException(302, "参数不正确");
        }
        OrgInfo orgDetail = orgInfoService.getOrgDetail(org.getOrgId());
        OrgInfoVo vo = dozerBeanMapper.map(orgDetail, OrgInfoVo.class);
        vo.setOrgLogoUrl(FtpClient.fullFtpUrl(vo.getOrgLogo()));

        CollectDto dto = new CollectDto(3,getLoginSessionUserId(),org.getOrgId(),null);
        List<CollectVo> collectVoList = collectService.selectCollectLikeList(dto);
        Map<Integer, CollectVo> groupby = collectVoList.stream().collect(Collectors.toMap(CollectVo::getRelateId, a -> a,(k1,k2)->k1));


            if(groupby!=null && groupby.get(vo.getOrgId())!=null) {
                vo.setCurrentCollect(groupby.get(vo.getOrgId()).getCurrentCollect());
                vo.setCurrentLike(groupby.get(vo.getOrgId()).getCurrentLike());
            }


        return ApiResult.success(vo);
    }
}

